﻿using System;
using System.Runtime.InteropServices;

namespace MediaRemoteServer
{
    public class LibVLC : IDisposable
    {
        [DllImport("libvlc")]
        private static extern IntPtr libvlc_new(int argc, string[] argv, IntPtr exception);

        [DllImport("libvlc")]
        private static extern void libvlc_exception_init(IntPtr p_exception);

        [DllImport("libvlc")]
        private static extern void libvlc_exception_clear(IntPtr p_exception);

        [DllImport("libvlc")]
        private static extern void libvlc_release(IntPtr p_instance);

        public struct libvlc_exception_t
        {
            public int b_raised;
        }

        public IntPtr instance { get; private set; }
        public IntPtr pException { get; private set; }

        public LibVLC()
        {
            pException = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(libvlc_exception_t)));
            libvlc_exception_init(pException);

            string[] args = { "--plugin-path=C:\\Program Files (x86)\\VideoLAN\\VLC\\plugins" };
            instance = libvlc_new(args.Length, args, pException);
        }

        public void clearException()
        {
            libvlc_exception_clear(pException);
        }

        #region IDisposable Members

        public void Dispose()
        {
            libvlc_release(instance);
        }

        #endregion
    }
}
